package com.ybf.api.controller;

import com.ybf.api.common.ResultResponse;
import com.ybf.api.entity.PageRequest;
import com.ybf.api.entity.RolePermissions;
import com.ybf.api.entity.User;
import com.ybf.api.service.RolePermissionsService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;

import static com.ybf.api.utils.Utils.mapToObject;

/**
 * @author ybf
 * @since 2024-09-26
 */
@Controller
@RequestMapping("/rolepermissions")
public class RolePermissionsController {

    @Resource
    RolePermissionsService resService;

    @Operation(summary = "获取列表")
    @GetMapping("/list")
    @ResponseBody
    public ResultResponse list(
            @RequestBody Map reqBody
    ) {
        RolePermissions rolepermissions = (RolePermissions) mapToObject(RolePermissions.class, reqBody);
        PageRequest pageRequest = (PageRequest) mapToObject(PageRequest.class, reqBody);
        return ResultResponse.ok(resService.list(rolepermissions, pageRequest));
    }

    @Operation(summary = "获取详情")
    @GetMapping("/info")
    @ResponseBody
    public ResultResponse info(
            @RequestBody(required = true) Map req
    ) {
        RolePermissions rolepermissions = resService.info((Integer) req.get("id"));
        return rolepermissions != null ? ResultResponse.ok(rolepermissions) : ResultResponse.error("error");
    }

    @Operation(summary = "删除")
    @GetMapping("/delete")
    @ResponseBody
    public ResultResponse delete(@RequestBody Map req) {
        Boolean status = resService.delete((Integer) req.get("id"));
        return status ? ResultResponse.ok(true) : ResultResponse.error(false);
    }

    @Operation(summary = "编辑详情", description = "不传id就是新增，传了就是编辑")
    @PostMapping("/edit")
    @ResponseBody
    public ResultResponse edit(@Valid @RequestBody(required = true) RolePermissions rolepermissions) {
        Integer status = resService.edit(rolepermissions);
        return status > 0 ? ResultResponse.ok(status) : ResultResponse.error(false);
    }
}
